//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverSetupArrest.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_arrested_msg","_die_msg","_shared_arrested","_shared_alive","_arrested_code","_die_code","_destination","_evt_idx"];
DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);

if (!isnull _object) then {
	_action_id = [_action] call FFUNC(actionGetID);
	TRACE_1("Action",_action);
	_parameters = [_action] call FFUNC(actionGetParams);
	
	_arrested_msg = [_parameters, "ARREST_MSG", "",THIS_FILE] call FFUNC(hashGet);
	_die_msg = [_parameters, "DIE_MSG", "",THIS_FILE] call FFUNC(hashGet);
	_shared_arrested = [_parameters, "SHARED_ARREST_VAR", "",THIS_FILE] call FFUNC(hashGet);
	_shared_alive = [_parameters, "SHARED_ALIVE_VAR", "",THIS_FILE] call FFUNC(hashGet);
	_arrested_code = [_parameters, "ARREST_CODE", "",THIS_FILE] call FFUNC(hashGet);
	_die_code = [_parameters, "DIE_CODE", "",THIS_FILE] call FFUNC(hashGet);
	_shared_arrested = _shared_arrested call FFUNC(getStringVar);
	_shared_alive = _shared_alive call FFUNC(getStringVar);

	if (!isNil "_shared_arrested") then {
		if (_shared_arrested != "") then {
			[_instance_id,_shared_arrested,false] spawn FFUNC(setMissionJIPVar);
			[QGVAR(all_var), [_instance_id,_shared_arrested,false],THIS_FILE] call FFUNC(callEvent);
		};
	};

	if (!isNil "_shared_alive") then {
		if (_shared_alive != "") then {
			[_instance_id,_shared_alive,(alive _object)] spawn FFUNC(setMissionJIPVar);
			[QGVAR(all_var), [_instance_id,_shared_alive,(alive _object)],THIS_FILE] call FFUNC(callEvent);
		};
	};

	_evt_idx = _object addEventHandler ["killed", Format["[_this, %1] call %2;",_action_id,QUOTE(FFUNC(eventObjectiveArrestFailed))]];
	[_object,_action,"ARREST_KILLED_EVENT",_evt_idx] call FFUNC(actionSetLocal);
	
	// This will trigger the arrest event.
	[_object,_action] spawn {
		private ["_person","_far_entities","_close_entities","_wait","_players_side","_action","_action_id"];
		DEFAULT_PARAM(_person,0,objNull);
		DEFAULT_PARAM(_action,1,[]);
		_action_id = [_action] call FFUNC(actionGetID);
		// We check that there are either no more friendlies in the vicinity or the person is dead or wounded.
		_wait = true;
		_players_side = switch (GVAR(own_side)) do {case "EAST": {east};case "WEST": {west};case "GUER": {resistance};case "CIV": {west}; default{west};};
		while {_wait && (alive _person)} do {
			sleep 10;
			_far_entities = (getPos _person) nearEntities [["Man"],100];
			_close_entities = (getPos _person) nearEntities [["Man"],10];
			//if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: Entities: %4  (%5)",diag_tickTime,"mission_functions.sqf","XfMissionObjectServerSetupArrest",_entities,(((side _person) countSide _entities) > 0)];};
			// The person which needs to be arrested has to be damaged a little and also there should be no alive allies in a radius of 200m
			_wait = (
				(
					// As long as there are friendlies less than 100m away even with enemies in the 10m
					(
						(_person countFriendly _far_entities) > 1
					)
					OR
					(
						(_players_side countSide _close_entities) <= 0
					)
				)
				AND
					// As long as it is alive
				(alive _person)
				AND
				(
					// As long as it is not damaged
					(damage _person) < 0.1
				)
			);
		};
		if (alive _person) then {
			[[_person],_action_id] call FFUNC(eventObjectiveArrestSuccess);
		};
	};
} else {
	ERROR("Object not set");
};
TRACE("End");
TMFPROFILERSTOP;